﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DroidLord.Util
{
    class MyUtils
    {

        public static bool IsUrl(string str)
        {
            try
            {
                string Url = @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
                return Regex.IsMatch(str, Url);
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public static string  getCurYMD()
        {
            DateTime dtNow = DateTime.Now;
            return dtNow.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
        }

        public static string getCurYMDHMS()
        {
            DateTime dtNow = DateTime.Now;
            return dtNow.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
        }


        public static string ConvertStringToDateTime( string timeStamp)
        {
            Int64 begtime = Convert.ToInt64(timeStamp) * 10000000;
            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度
            DateTime dt = new DateTime(time_tricks);//转化为DateTime
            return dt.ToString("yyyy-MM-dd HH:mm");
        }

        /*
        public static Boolean isOutOfDay()
        {
            DateTime dtNow = DateTime.Now;
            int subDay = 0;
          
            DateTime endTime = DateTime.Parse("2030-8-15");
            TimeSpan timeSpan = endTime.Subtract(dtNow);
            subDay = timeSpan.Days;
            if(subDay<=0)
            {
                return true;
            }
            return false;
        }
        */


        public static string GetFileMD5(string filepath)
        {
            FileStream fs = new System.IO.FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
            int bufferSize = 1048576;
            byte[] buff = new byte[bufferSize];
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.Initialize();
            long offset = 0;
            while (offset < fs.Length)
            {
                long readSize = bufferSize;
                if (offset + readSize > fs.Length)
                    readSize = fs.Length - offset;
                fs.Read(buff, 0, Convert.ToInt32(readSize));
                if (offset + readSize < fs.Length)
                    md5.TransformBlock(buff, 0, Convert.ToInt32(readSize), buff, 0);
                else
                    md5.TransformFinalBlock(buff, 0, Convert.ToInt32(readSize));
                offset += bufferSize;
            }
            if (offset >= fs.Length)
            {
                fs.Close();
                byte[] result = md5.Hash;
                md5.Clear();
                StringBuilder sb = new StringBuilder(32);
                for (int i = 0; i < result.Length; i++)
                    sb.Append(result[i].ToString("X2"));
                return sb.ToString();
            }
            else
            {
                fs.Close();
                return null;
            }
        }

    }
}
